package org.llw.ldailytask.ui;

import java.util.Calendar;

import org.llw.ldailytask.R;
import org.llw.ldailytask.dao.SqliteDao;
import org.llw.ldailytask.entity.Item;
import org.llw.ldailytask.entity.Task;
import org.llw.ldailytask.util.CallBack;
import org.llw.ldailytask.util.MySpinnerAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class DetailActivity extends Activity implements OnClickListener,
		OnItemSelectedListener {

	private TextView cancel, ok, date, time;
	private EditText title, content;
	private Bundle bundle;
	private String currentHour = "00", currentMin = "00";
	private String currentYear, currentMonth, currentDay, currentTime;
	private Spinner spinnerType, spinnerIsalert;
	private String type, isAlert;
	private CallBack callBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		bundle = this.getIntent().getExtras();
		
		init();

		dateInit(null, null, null, null, 0, 0);

	}

	private void init() {
		ok = (TextView) findViewById(R.id.txt_activity_detail_ok);
		cancel = (TextView) findViewById(R.id.txt_activity_detail_cancel);
		this.date = (TextView) findViewById(R.id.txt_activity_detail_date);
		this.time = (TextView) findViewById(R.id.txt_activity_detail_time);
		this.title = (EditText) findViewById(R.id.edit_activity_detail_title);
		this.content = (EditText) findViewById(R.id.edit_activity_detail_content);
		spinnerType = (Spinner) findViewById(R.id.spinner_activity_detail_type);
		spinnerIsalert = (Spinner) findViewById(R.id.spinner_activity_detail_isalert);
		spinnerType.setOnItemSelectedListener(this);
		spinnerIsalert.setOnItemSelectedListener(this);
		spinnerType.setAdapter(new MySpinnerAdapter(this, new Item[] {
				new Item(R.drawable.img_general, "一般"),
				new Item(R.drawable.img_urgent, "紧急"),
				new Item(R.drawable.img_nothing, "仅标记") }));
		spinnerIsalert.setAdapter(new MySpinnerAdapter(this, new Item[] {
				new Item(R.drawable.img_mute, "不提醒"),
				new Item(R.drawable.img_clock, "提醒") }));
		ok.setOnClickListener(this);
		cancel.setOnClickListener(this);
		this.date.setOnClickListener(this);
		this.time.setOnClickListener(this);
	}

	private void dateInit(String date, String time, String title,
			String content, int type, int isalert) {
		String[] dateArray = bundle.getString("currentDate").split("-");
		currentYear = dateArray[0];
		currentMonth = dateArray[1];
		currentDay = dateArray[2];

		this.date.setText(bundle.getString("currentDate")
				+ " "
				+ getDay(Integer.valueOf(dateArray[0]),
						Integer.valueOf(dateArray[1]),
						Integer.valueOf(dateArray[2])));
		this.time.setText("00:00");

	}

	private String getDay(int year, int month, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, day);
		int number = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		String[] str = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", };
		return str[number];
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.txt_activity_detail_ok:
			addTask();
			Toast.makeText(this, "成功添加一条新事项", Toast.LENGTH_SHORT).show();
			DetailActivity.this.finish();
			break;
		case R.id.txt_activity_detail_cancel:
			new AlertDialog.Builder(this)
					.setTitle("您还没有保存，确认退出编辑吗？")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// 点击“确认”后的操作
									DetailActivity.this.finish();

								}
							})
					.setNegativeButton("返回",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// 点击“返回”后的操作,这里不设置没有任何操作
								}
							}).show();
			break;
		case R.id.txt_activity_detail_date:
			DatePickerDialog datePickerDialog = getDatePickerDialog();
			datePickerDialog.setTitle("设定日期");
			datePickerDialog.show();
			break;
		case R.id.txt_activity_detail_time:
			TimePickerDialog timePickerDialog = getTimePickerDialog();
			timePickerDialog.setTitle("设定时间");
			timePickerDialog.show();
			break;
		}

	}

	private DatePickerDialog getDatePickerDialog() {
		return new DatePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT,
				new OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						currentYear = String.valueOf(year);
						currentMonth = String.valueOf(monthOfYear + 1);
						currentDay = String.valueOf(dayOfMonth);
						date.setText(year + "-" + (monthOfYear + 1) + "-"
								+ dayOfMonth + " "
								+ getDay(year, (monthOfYear + 1), dayOfMonth));
					}
				}, Integer.valueOf(currentYear),
				Integer.valueOf(currentMonth) - 1, Integer.valueOf(currentDay));
	}

	private TimePickerDialog getTimePickerDialog() {
		return new TimePickerDialog(
				this,
				AlertDialog.THEME_HOLO_LIGHT,
				new OnTimeSetListener() {
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay,
							int minute) {
						currentHour = (String.valueOf(hourOfDay).length()) == 1 ? (0 + String
								.valueOf(hourOfDay)) : (String
								.valueOf(hourOfDay));
						currentMin = (String.valueOf(minute).length()) == 1 ? (0 + String
								.valueOf(minute)) : (String.valueOf(minute));
						time.setText(currentHour + ":" + currentMin);
					}
				}, Integer.valueOf(currentHour), Integer.valueOf(currentMin),
				true);
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {

		type = ((TextView) spinnerType.getSelectedView().findViewById(
				R.id.txt_item)).getText().toString();

		isAlert = ((TextView) spinnerIsalert.getSelectedView().findViewById(
				R.id.txt_item)).getText().toString();

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBackPressed() {
		new AlertDialog.Builder(this).setTitle("您还没有保存，确认退出编辑吗？")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 点击“确认”后的操作
						
						
						
						
						DetailActivity.this.finish();

					}
				})
				.setNegativeButton("返回", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 点击“返回”后的操作,这里不设置没有任何操作
					}
				}).show();
	}

	private void addTask() {
		SqliteDao sd = new SqliteDao(this);
		String date = currentYear + "-" + currentMonth + "-" + currentDay;
		String time = date + " " + currentHour + ":" + currentMin + ":00";
		sd.addTask(date, time, title.getText().toString(), content.getText()
				.toString(), type, "0", isAlert);

	}
}
